package com.jsptpd.smartauth.common.config.swagger;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableWebMvc
@Profile({"dev", "test"}) //swagger仅在dev,test配置中生效，在生产环境pro下不开启;或者通过配置enable
public class Swagger2Config extends WebMvcConfigurerAdapter {

    //@Value("${config.swagger2.enable}")
    //private boolean swaggerEnable;
    @Value("${spring.resources.static-locations}")
    private String staticLocation;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/**")
                .addResourceLocations(staticLocation);

        super.addResourceHandlers(registry);
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //.enable(swaggerEnable)//是否开启swagger
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.jsptpd.smartauth.modules.business.restcontrol"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(getHeaders());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SmartAuth-统一认证登录平台 Swagger2的API测试页面")
                .description("部分API需要设置appId appKey 以及token，请联系管理员获取参数")
                .termsOfServiceUrl("http://58.240.86.44:23588/smartauth/")
                .version("0.3")
                .build();
    }

    /**实现点：业务系统的token认证机制**/
    private List<Parameter> getHeaders() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization").description("JWT").modelRef(new ModelRef("string"))
                .defaultValue("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBOYW1lIjoiY29tLmpzcHRwZC5zcmZhIiwiZXhwIjoxNTU2MzQwOTIxLCJ1c2VySWQiOiI4ODkyOTMiLCJpYXQiOjE1NDc3MDA5MjF9.GUq7PSfIrDmywB9bTBcZMZnDG6J9uJSrsNHf5ultl_g")
                .parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        tokenPar.name("appId").description("app").modelRef(new ModelRef("string"))
                .parameterType("header").defaultValue("jsptpd.xlck.app.android").required(false).build();
        pars.add(tokenPar.build());
        tokenPar.name("appKey").description("app").modelRef(new ModelRef("string"))
                .parameterType("header").defaultValue("35g4dPb959Fy08fv").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }

}

